<?php
/**
 * @description : the send mail class.
 * @author : stephen.mo
 * @date : 2010-06-11
 */

require_once 'PHPMailer/class.phpmailer.php';

class MstatSendEmail {
    
    static public function getInstance() {
        static $instance = null ;
        return $instance ? ($instance) : ($instance = new MstatSendEmail()) ;
    }
    
    public function sendMailProcess ($subject, $mail_body, $user_email, $body_format='1') {
        //object.		
        $mail = new PHPMailer();
		
        //smtp setting.
        $smtpinfo = ModelConfig::getSmtpSetting();
		if ($smtpinfo['issmtp']) {
			$mail->IsSMTP();
			if ($smtpinfo['isauth']) {
				$mail->SMTPAuth = true;                     //enable SMTP authentication
				$mail->Username = $smtpinfo['username'];    //user name.
				$mail->Password = $smtpinfo['password'];    //user pass.
			}
            else {
				$mail->SMTPAuth = false;                    //disable SMTP authentication
			}
			$mail->Host = $smtpinfo['host'];                //sets GMAIL as the SMTP server
			$mail->Port = $smtpinfo['port'];                //set the SMTP port for the GMAIL server
		}
		
		$mail->FromName = "TVB.COM";                        //from name.
		$mail->Subject = $subject;                          //mail's subject.
		if ($body_format == '1') {                          //format.
			$mail->MsgHTML($mail_body);
		}
        else {
			$mail->IsHTML(false);
			$mail->Body = $mail_body;
		}
		$mail->CharSet = 'utf-8';                          //charset.
		$mail->AddAddress($user_email);                    //user mail list.
        
        return ($mail->Send()) ? true : false ;         
    }
    
    //setting subjects.
    public function setMailSubject($action,$date) {
        switch($action) {
            case 'DailySummary':
                return '[mytv+] Daily Automated Executive Summary Report  ('.$date.')';
                break;
            default:
                return null;
        }
    }
    
    //setting mail body.
    public function setMailBody($data,$action) {
        $result = null;
        switch($action) {
            case 'DailySummary':
                $package_arr = ModelConfig::getConfigSetting('plan_package');
                $result = 'Dear {XXXX}'."\n\n";
                $result .= "Daily Automated Executive Summary Report\n\n";
                foreach($data as $key=>$val) {
                    if ($key=='Total Streaming Duration') {
                        $tmp = null;
                        foreach($val as $list_bitrate) {
                            $tmp.="\t".$list_bitrate['bitrate'].'K - '.number_format($list_bitrate['Total'])."\t\n" ;
                        }
                        $result .= $key." : \n".$tmp."\n";
                        continue ;
                    }
                    
                    if($key=='Subscriber with package') {
                        $tmp = null;
                        foreach($val as $package_id=>$list) {
                            foreach($list as $item=>$item_val) {
                                $tmp.="\t".$package_arr[$package_id]."\t".$item." :\t".number_format($item_val)."\n";
                            }
                            $tmp.="\n";
                        }
                        $result .= $key." : \n".$tmp."\n";
                        continue ;
                    }
                    $result .= $key." : ".number_format($val)."\n";
                }
                break ;
            default:
                return null;
        }

        return $result;
    }


}

?>